ESP32: mDNS address resolution

您所在的位置:网站首页 Esp32 dns server 拖慢响应 ESP32: mDNS address resolution

ESP32: mDNS address resolution

2024-07-16 15:24:03| 来源: 网络整理| 查看: 265

In this tutorial we will learn how to use mDNS to resolve the address of a HTTP web server hosted by the ESP32. The tests from this tutorial were done using a DFRobot’s ESP32 module integrated in a ESP32 development board.

In this tutorial we will learn how to use mDNS to resolve the address of a HTTP web server hosted by the ESP32.

For this example, I’m assuming the use of the HTTP async web server library, which was covered in detail on this previous tutorial. The mentioned tutorial explains how to install the library and how to get started using it.

In order to reach the ESP32 server, we need to know its IP address and, in most examples we have covered so far, we print that address to the console and then we use it in our browser.

Nonetheless, in more realistic scenarios, printing the IP address might not be an option. Thus, mDNS is a protocol that allows to make the resolution of locally defined names to IPs without the need for dedicated infra-structures (such as a DNS server) [1]. You can read the specification of the protocol here.

In other words, we can use a name instead of an IP in the URL to access the browser, and mDNS will allow to resolve this name into an IP address.

This protocol operates over multicast UDP [2]. Note that the “m” from mDNS stands for “multicast” [1].

It’s important to take in consideration that the device that is reaching the server also needs mDNS. I’ve tested with a Windows 10 machine and it is able to perform the resolution of the address. On older operating systems you might need to install additional software to be able to do the resolution.

Note that we don’t need to install any additional library for mDNS since it is already included with the Arduino core. The code shown here is based on the examples from the Arduino core, which I recommend you to check.

The tests from this tutorial were done using a DFRobot’s ESP32 module integrated in a ESP32 development board.

If you prefer a video version of this tutorial, please check my YouTube channel below:

The code

We will start the code by the library includes. We will need to include the ESPmDNS.h library, so we have access to the mDNS related functionalities.

Additionally, we will need the WiFi.h, to be able to connect the ESP32 to a WiFi network, and the ESPAsyncWebServer.h, so we can setup a HTTP web server to run on the ESP32.

#include #include #include

Then we will declare two variables to hold the WiFi network credentials: network name and password.

const char* ssid = "yourNetworkName"; const char* password = "yourNetworkPass";

We will also define a variable of class AsyncWebServer, which we will use to configure our server. As input of the constructor of this class we need to pass the number of the port where the ESP32 will be listening to incoming requests. We will use port 80, which is the default HTTP port.

AsyncWebServer server(80);

Moving on to the Arduino setup, we will start by opening a serial connection and then we will connect the ESP32 to the WiFi network, using the credentials previously defined.

Serial.begin(115200); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.println("Connecting to WiFi.."); }

Then we will take care of setting up the mDNS responder. To do so, we simply need to call the begin method on the MDNS extern variable. This variable is an object of class MDNSResponder that becomes available when we include the ESPmDNS.h library.

As input of the begin method, we need to pass a string with the hostname that we want to be resolved to the address of the ESP32. We will set this name to “esp32“.

As output, the begin method returns a Boolean value indicating if the setup procedure was successful or not. We will use that value for error checking.

if(!MDNS.begin("esp32")) { Serial.println("Error starting mDNS"); return; }

If this setup occurs successfully, then we will take care of setting up the HTTP server. We will configure a route called “/hello” that will listen to HTTP GET requests and return a simple “Hello World” message.

server.on("/hello", HTTP_GET, [](AsyncWebServerRequest *request){ request->send(200, "text/plain", "Hello World"); });

To finalize, we simply need to call the begin method on our server object, to make it ready to listen to incoming request. The full setup function can be seen below and already includes the call to the begin method.

void setup(){ Serial.begin(115200); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.println("Connecting to WiFi.."); } if(!MDNS.begin("esp32")) { Serial.println("Error starting mDNS"); return; } Serial.println(WiFi.localIP()); server.on("/hello", HTTP_GET, [](AsyncWebServerRequest *request){ request->send(200, "text/plain", "Hello World"); }); server.begin(); }

Since we are working with an asynchronous web server, it means we don’t need to poll any object periodically in the Arduino main loop. Thus, for our example, it can be left empty.

void loop(){}

The final code can be seen below.

#include #include #include const char* ssid = "yourNetworkName"; const char* password = "yourNetworkPass"; AsyncWebServer server(80); void setup(){ Serial.begin(115200); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.println("Connecting to WiFi.."); } if(!MDNS.begin("esp32")) { Serial.println("Error starting mDNS"); return; } Serial.println(WiFi.localIP()); server.on("/hello", HTTP_GET, [](AsyncWebServerRequest *request){ request->send(200, "text/plain", "Hello World"); }); server.begin(); } void loop(){} Testing the code

To test the code, simply compile it and upload it to your device using the Arduino IDE. When the procedure finishes, open the Serial monitor and wait for the IP address of the server to be printed. Note that we won’t need it to access the server, we are just printing it to confirm a local address was assigned to the ESP32.

Then, open a web browser of your choice and type the following in the address bar:

http://esp32.local/hello

After accessing the previous URL, the browser should reach the server and receive as output the “Hello World” message we have defined in the code. You can check the expected result in figure 1.

Message returned by the ESP32 server.Figure 1 – Message returned by the ESP32 server. References

[1] https://tools.ietf.org/html/rfc6762

[2] https://stackoverflow.com/questions/11835782/how-exactly-does-mdns-resolve-addresses



【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


图片新闻

实验室药品柜的特性有哪些
实验室药品柜是实验室家具的重要组成部分之一,主要
小学科学实验中有哪些教学
计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
实验室各种仪器原理动图讲
1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
高中化学常见仪器及实验装
1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
微生物操作主要设备和器具
今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
浅谈通风柜使用基本常识
 众所周知,通风柜功能中最主要的就是排气功能。在

专题文章

    CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭